package com.watf.functions.gs;

/*
 * FileName: XMLRulesFuncs.java
 * Author:Jester
 * Methods:XMLRuleNew()  
 * 
 * History:
 * Date       Comments       Updater
 * 2011-5-30  First Version  Jester
 */

import org.testng.Assert;
import org.testng.Reporter;

import com.thoughtworks.selenium.Selenium;
import com.watf.functions.core.DefaultBasicFuncs;
import com.watf.pages.gs.MainFrame;
import com.watf.pages.gs.XMLRules;

import java.awt.event.KeyEvent;

public class XMLRulesFuncs extends DefaultBasicFuncs {
	
	
	public void gotoXMLRulesPage( Selenium selenium ) {
		selenium.click( MainFrame.DataSources_MENU );
		selenium.click( MainFrame.XMLRules_SUBMENU );
		selenium.waitForPageToLoad( CommonFuncs.SHORT_WAIT );
		Assert.assertEquals( selenium.getTitle(), XMLRules.TITLE );
	}

	/**
	 * Create a new XML Rule
	 * @return true if the new XML rule is created rightly;<b>
	 * 		   false if the rule is invalid, or a rule with the same name already exists.
	 */
	public boolean newXMLRule(Selenium selenium, String iRuleName, String iRule)
			throws Exception {
		
		selenium.click(XMLRules.New_BUTTON);
		selenium.waitForPageToLoad(CommonFuncs.SHORT_WAIT);

		selenium.type(XMLRules.Name_TEXT_FIELD, iRuleName);
		selenium.type(XMLRules.Rules_TEXT_FIELD, iRule);

		// validate the rule
		if( validateRule( selenium ) == false ) {
			return false;
		}
		
		selenium.click( XMLRules.Save_BUTTON );
		if (selenium.isAlertPresent()) {    // to handle the situation that the XML rule already exists
			Reporter.log( selenium.getAlert() ); 
			selenium.click(XMLRules.Cancel_BUTTON);
			selenium.waitForPageToLoad( CommonFuncs.SHORT_WAIT );
			return false;
		} else {
			selenium.waitForPageToLoad(CommonFuncs.SHORT_WAIT);
		}
		
		// Check if the Rule has been exists.
		Assert.assertTrue( isPresentInTable(selenium, XMLRules.XMLRules_TABLE, iRuleName),
				"Failed to create the xml rule!" );
		return true;
	}
	
	/**
	 * Edit a XML rule
	 * @param description Description of the XML rule
	 * @param iRule
	 * @return true if edit successfully; false if the XML rule does not exist, or the new rule is invalid.
	 */

	public boolean editXMLRule(Selenium selenium, String iRuleName, String description, String iRule) throws Exception {
		
		boolean selected = selectRadioButtonFromTable(selenium, XMLRules.XMLRules_TABLE, iRuleName);
		if (!selected) {
			Reporter.log("Cannot find the XML rule: " + iRuleName );
			return false;
		}
		
		selenium.click(XMLRules.Edit_BUTTON);
		selenium.waitForPageToLoad(CommonFuncs.SHORT_WAIT);
			
		selenium.type(XMLRules.Description_TEXT_FIELD, description);
		selenium.type(XMLRules.Rules_TEXT_FIELD, iRule);
		
		// validate the rule
		if( validateRule( selenium ) == false ) {
			return false;
		}
		
		selenium.click( XMLRules.Save_BUTTON );
		selenium.waitForPageToLoad( CommonFuncs.SHORT_WAIT );

		// Check if the rule has been edited.
		Assert.assertEquals(this.getColumnText(selenium,
				XMLRules.XMLRules_TABLE, iRuleName, 3), description);
		return true;
	}
	
	/**
	 * Handle the process of validating the XML rule
	 * @author bear
	 */
	private boolean validateRule( Selenium selenium ) {
		try {
			selenium.click(XMLRules.Validate_BUTTON);
			Thread.sleep( 5000 );    // sleep 5 seconds
			if ( selenium.isEditable( XMLRules.Save_BUTTON ) ) {    // Save button is editable, so the rule is valid
				// type 'Enter' key to dismiss the alert
				selenium.keyPressNative(  String.valueOf( KeyEvent.VK_ENTER ) );
				return true;
			} else {    // The XML rule is invalid, so to output the error message
				Reporter.log( "The XML rule is invalid, and the error message is:" );
				Reporter.log( selenium.getText( XMLRules.Error_MSG ) );
				return false;
			}
		} catch ( InterruptedException e ) {
			Reporter.log( e.getMessage() );
			return false;
		}
	}

}
